package com.jprocms.module.cms.config;

import cn.hutool.core.util.ObjectUtil;
import lombok.Data;

import java.io.File;

/**
 * @author jprocms
 */
@Data
public class TplLocalConfigProperties {

    /**
     * 站点的前缀
     */
    private String sitePrefix;

    /**
     * 模板存储|访问路径
     */
    private String path;

    /**
     * 首页模板的默认文件路径
     */
    private String indexPath;

    /**
     * 获取拼装之后的访问路径
     *
     * @param siteId
     * @param relativePath
     * @return
     */
    public String getRealPath(Long siteId, String relativePath, String separator) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(path);
        stringBuffer.append(separator);
        stringBuffer.append(sitePrefix);
        stringBuffer.append(siteId);
        if (ObjectUtil.isNotEmpty(relativePath)) {
            stringBuffer.append(separator);
            stringBuffer.append(relativePath);
        }
        return stringBuffer.toString();
    }

    public String getRelativePath(Long siteId, String relativePath) {
        String separator = File.separator;
        StringBuilder stringBuffer = new StringBuilder();
        //stringBuffer.append(separator);
        stringBuffer.append(File.separator);
        stringBuffer.append(sitePrefix);
        stringBuffer.append(siteId);
        if (ObjectUtil.isNotEmpty(relativePath)) {
            int index = relativePath.lastIndexOf(".");
            if (index > -1) {
                relativePath = relativePath.substring(0, index);
            }
            stringBuffer.append(relativePath);
        }
        return stringBuffer.toString().replace("\\", separator).replace("/", separator);
    }

    public String getPrefixPath(Long siteId, String solution) {
        StringBuilder stringBuffer = new StringBuilder();
        stringBuffer.append("/");
        stringBuffer.append(sitePrefix);
        stringBuffer.append(siteId);
        stringBuffer.append("/");
        stringBuffer.append(solution);
        stringBuffer.append("/");
        return stringBuffer.toString();
    }
}
